package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.commonutils.results;
import com.entity.pojo.CategoriesRelation;
import com.entity.pojo.Concept;
import com.entity.pojo.ConceptRelation;
import com.service.ConceptRelationService;
import com.service.ConceptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 关联知识点表（关联关系） 前端控制器
 * </p>
 *
 * @author chenMo
 * @since 2022-04-14
 */
@CrossOrigin
@RestController
@RequestMapping("/conceptRelation")
public class ConceptRelationController {
    @Resource
    private ConceptRelationService conceptRelationService;

    @Resource
    private ConceptService conceptService;

    @ApiOperation("根据当前知识点id查询关联知识点列表")
    @GetMapping("/getNextConceptList/{conceptId}")
    public results getNextConceptList(@PathVariable Integer conceptId) {
        List<ConceptRelation> conceptRelationList = conceptRelationService.list(new QueryWrapper<ConceptRelation>().eq("conceptId", conceptId));
        List<Integer> nextConceptIds = conceptRelationList.stream().map(ConceptRelation::getNextConceptId).collect(Collectors.toList());
        // 查询关联等级，用来动态渲染关联课程标签的颜色
        List<Integer> relationLevelList = conceptRelationList.stream().map(ConceptRelation::getRelationLevel).collect(Collectors.toList());
        if (!nextConceptIds.isEmpty()) {
            Collection<Concept> nextConceptList = conceptService.listByIds(nextConceptIds);
            return results.ok().data("nextConceptList", nextConceptList).data("relationLevelList",relationLevelList);
        }
        return results.ok().data("nextConceptList", null).data("relationLevelList",null);
    }

    @ApiOperation("根据知识点id查询可以添加的关联知识点列表")
    @GetMapping("/getCanBeAddedNextConceptList/{currentConceptId}/{categoryId}")
    public results getCanBeAddedNextConceptList(@PathVariable Integer currentConceptId,@PathVariable Integer categoryId) {
        List<Concept> canBeAddedNextConceptList = conceptRelationService.getCanBeAddedNextConceptList(currentConceptId,categoryId);
        return results.ok().data("canBeAddedNextConceptList", canBeAddedNextConceptList);
    }

    @ApiOperation("增加新的关联知识点")
    @PostMapping("addConceptRelation")
    public results addConceptRelation(@RequestBody ConceptRelation conceptRelation) {
        boolean saveFlag = conceptRelationService.addConceptRelation(conceptRelation);
        if (saveFlag) {
            return results.ok();
        } else {
            return results.error("增加关联知识点信息失败");
        }
    }

    @ApiOperation("删除关联目录")
    @DeleteMapping("deleteConceptRelation/{conceptId}/{nextConceptId}")
    public results deleteConceptRelation(@PathVariable Integer conceptId, @PathVariable Integer nextConceptId) {
        boolean deleteFlag = conceptRelationService.deleteConceptRelation(conceptId, nextConceptId);
        if (deleteFlag) {
            return results.ok();
        } else {
            return results.error("删除关联知识点失败");
        }
    }

}

